package com.test.wog.dao;

import com.test.wog.bean.Road;
import com.test.wog.util.GeometryTrimUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author LX
 * @since 2020-01-28
 *  关键通道得数据库操作
 */
@Repository
public class RoadDAOImpl implements RoadDAO{
    @Autowired
    JdbcTemplate jdbc;
    @Autowired
    GeometryTrimUtil gtu;


    @Override
    public List<Road> getRoadByType(String type) {
        String sql = "select id, name, type, length, duration, " + gtu.trimLine("ST_ASTEXT(geom)") + " as geom from critical_road where type = ?";
        List<Road> roadList = jdbc.query(
                sql,
                new Object[]{type},
                new BeanPropertyRowMapper(Road.class)
        );
        return roadList;
    }

    @Override
    public List<Road> getRoadByName(String name) {
        String sql = "select id, name, type, length, duration, " + gtu.trimLine("ST_ASTEXT(geom)") + " as geom from critical_road where name = ?";
        List<Road> roadList = jdbc.query(
                sql,
                new Object[]{name},
                new BeanPropertyRowMapper(Road.class)
        );
        return roadList;
    }

    @Override
    public List<Road> getAllRoad() {
        String sql = "select id, name, type, length, duration, " + gtu.trimLine("ST_ASTEXT(geom)") + " as geom from critical_road";
        List<Road> roadList = jdbc.query(
                sql,
                new Object[]{},
                new BeanPropertyRowMapper(Road.class)
        );
        return roadList;
    }


}
